/**
 * 
 */
package com.google.code.maven.plugin.http.client.config;

import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import com.google.code.maven.plugin.http.client.Credentials;
import com.google.code.maven.plugin.http.client.Proxy;

/**
 * Proxy
 * 
 * @author Nadim Benabdenbi
 * @version 1.0
 * @since JDK1.6
 */
public class ProxyConfiguration extends Proxy implements ConfigurationBean<Proxy> {
	
	protected static final AtomicInteger id = new AtomicInteger();
	
	/**
	 * @parameter
	 */
	private CredentialsConfiguration credentials;
	
	public Proxy build(DefaultListableBeanFactory factory) {
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Proxy.class);
		String beanId = Credentials.class.getSimpleName() + id.incrementAndGet();
		builder.addPropertyValue("host", getHost());
		builder.addPropertyValue("port", getPort());
		builder.addPropertyValue("credentials", credentials == null ? null : credentials.build(factory));
		factory.registerBeanDefinition(beanId, builder.getBeanDefinition());
		return factory.getBean(beanId, Proxy.class);
	}
	
	/**
	 * @return the credentials
	 */
	@Override
	public CredentialsConfiguration getCredentials() {
		return credentials;
	}
	
	/**
	 * @param credentials
	 *            the credentials to set
	 */
	public void setCredentials(CredentialsConfiguration credentials) {
		this.credentials = credentials;
	}
	
}
